//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CA = ComponentArt.Web.UI;

namespace CommunityServerWeb.ControlPanel.Tools.Reports
{
	public class PollVotingReportList : BaseToolsGridControl
	{
		protected CA.Grid Grid1;

		private void Page_Load(object sender, EventArgs e)
		{
			this.CurrentGridRowSize = 1;

			if (!this.IsPostBack && !Grid.IsCallback)
			{
				buildGrid();
				Grid.DataBind();
			}
		}

		protected override void buildGrid()
		{
			CSContext cntx = CSContext.Current;
			int postID = cntx.GetIntFromQueryString("pid",-1);
			int applicationTypeID = cntx.GetIntFromQueryString("atid",-1);

			if (postID == -1 || applicationTypeID == -1)
				return;

			CSApplicationData appData = ApplicationSet.Applications[(ApplicationType) applicationTypeID];
			if (appData == null)
				return;

			Post post = appData.GetPost(postID);
			if (post == null)
				return;

			PollSummary poll = Polls.GetPoll(post);

			this.GridMode = CA.GridRunningMode.Callback;
			PollVoteSet pvs;
			if(this.GridMode == CA.GridRunningMode.Callback)
			{
				Grid.ManualPaging = true;
				pvs = poll.GetUserVotes(Grid.CurrentPageIndex, this.DefaultPageSize);
			}
			else
				pvs = poll.GetUserVotes(0, int.MaxValue - 1); 

			this.Grid.DataSource = pvs.PollVotes;
			this.RecordCount = pvs.TotalRecords;
			base.buildGrid();
		}

		protected override void ConfigureGrid()
		{
			AddGridPagerTemplate("DownloadDate");
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new EventHandler(this.Page_Load);
		}

		#endregion
	}
}
